package com.cxy.springbootelasticjob.job;

import com.cxy.autoconfig.ElasticDataflowJob;
import com.cxy.springbootelasticjob.dao.JdOrderMapper;
import com.cxy.springbootelasticjob.dao.TmallOrderMapper;
import com.cxy.springbootelasticjob.dao.my.MyJdOrderMapper;
import com.cxy.springbootelasticjob.dao.my.MyTmallOrderMapper;
import com.cxy.springbootelasticjob.model.AllOrder;
import com.cxy.springbootelasticjob.model.JdOrder;
import com.cxy.springbootelasticjob.model.TmallOrder;
import com.cxy.springbootelasticjob.service.OrderService;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: huayushu luming
 * @date: 2020-11-06 02:17
 * @desc:
 **/
//@ElasticDataflowJob(
//        jobName = "fetchThirdOrderJob",
//        cron = "0/15 * * * * ?",
//        shardingTotalCount = 2,
//        overwrite = true,
//        streamingProcess = true
//)
public class FetchThirdOrderJob implements DataflowJob<Object> {
    @Autowired
    private MyJdOrderMapper myJdOrderMapper;
    @Autowired
    private MyTmallOrderMapper myTmallOrderMapper;
    @Autowired
    private OrderService orderService;
    @Override
    public List<Object> fetchData(ShardingContext shardingContext) {
        if(shardingContext.getShardingItem()==0){
            //京东订单
            List<JdOrder> jdOrdersDb = myJdOrderMapper.getNotFetchedOrder(5);
            if(jdOrdersDb!=null&&jdOrdersDb.size()>0){
                List<Object>jdOrderList=jdOrdersDb.stream().map(jdOrder -> (Object)jdOrder).collect(Collectors.toList());
                return  jdOrderList;
            }

        }else {
            //天猫订单
            List<TmallOrder> tmallOrdersDb = myTmallOrderMapper.getNotFetchedOrder(5);
            if(tmallOrdersDb!=null&&tmallOrdersDb.size()>0){
                List<Object> tmallOrderlist = tmallOrdersDb.stream().map(tmallOrder -> (Object) tmallOrder).collect(Collectors.toList());
                return tmallOrderlist;
            }
        }
        return null;
    }

    @Override
    public void processData(ShardingContext shardingContext, List<Object> list) {
        //入库
        //京东订单
        if(shardingContext.getShardingItem()==0){
            if(list!=null&&list.size()>0){
                List<JdOrder> jdOrders = list.stream().map(d -> (JdOrder) d).collect(Collectors.toList());
                for (JdOrder jdOrder : jdOrders) {
                    AllOrder allOrder = new AllOrder();
                    allOrder.setThirdOrderId(jdOrder.getId());
                    allOrder.setType(0);//京东订单
                    allOrder.setTotalAmount(jdOrder.getAmount());
                    allOrder.setCreateUser("system");
                    allOrder.setUpdateUser("system");
                    allOrder.setUpdateTime(new Date());
                    allOrder.setCreateTime(new Date());
                    orderService.processJdOrder(allOrder);
                }
            }
        }else {
            //天猫订单
            if(list!=null&&list.size()>0){
                List<TmallOrder> tmallOrders = list.stream().map(d -> (TmallOrder) d).collect(Collectors.toList());
                for (TmallOrder tmallOrder : tmallOrders) {
                    AllOrder allOrder = new AllOrder();
                    allOrder.setThirdOrderId(tmallOrder.getId());
                    allOrder.setType(1);//天猫订单
                    allOrder.setTotalAmount(tmallOrder.getMoney());
                    allOrder.setCreateUser("system");
                    allOrder.setUpdateUser("system");
                    allOrder.setUpdateTime(new Date());
                    allOrder.setCreateTime(new Date());
                    orderService.processTmallOrder(allOrder);
                }
            }
        }

    }
}
